class UsersController < ApplicationController
  before_filter :find_current_user

  # GET /users/1
  # GET /users/1.xml
  def show
    @user = User.find(params[:id])
    @user_info = UserInfo.find_or_create_by_user_id(@user.id)
    @learned_courses = @user.learned_courses
    respond_to do |format|
      format.xml  { render :xml => @user }
      format.html
    end
  end

  # GET /users/1/edit
  def edit
    @user = User.find(params[:id])
  end

  # POST /users
  # POST /users.xml
  def create
    @user = User.new(params[:user])
    respond_to do |format|
      if @user.save
        flash[:notice] = 'User was successfully created.'
        format.html { redirect_to(@user) }
        format.xml  { render :xml => @user, :status => :created, :location => @user }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /users/1
  # PUT /users/1.xml
  def update
    @user = User.find(params[:id])
    respond_to do |format|
      if @user.update_attributes(params[:user])
        flash[:notice] = 'User was successfully updated.'
        format.html { redirect_to(@user) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @user.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /users/1
  # DELETE /users/1.xml
  def destroy
    @user = User.find(params[:id])
    @user.destroy
    respond_to do |format|
      format.html { redirect_to(users_url) }
      format.xml  { head :ok }
    end
  end

  def edit_profile
    @user_info = UserInfo.find_or_create_by_user_id(@current_user.id)
    respond_to do |format|
      format.html
      # format.js { render_to_facebox}
      format.js {render_to_facebox}
    end
  end

  def update_user_profile
    @user_info = UserInfo.find_or_create_by_user_id(@current_user.id)
    begin
      @user_info.attributes = params[:user_info]
      if @user_info.save
        flash.now[:notice] = "个人信息修改成功."
        respond_to do |format|
          format.html{redirect_to edit_profile_user_path}
          format.js{}
        end
      else
        flash.now[:notice] = "个人信息修改失败."
      end
    end
  end

  def  edit_image
    @user_info = UserInfo.find_or_create_by_user_id(@current_user.id)
    @user_image = UserImage.new
  end

  def  update_image
    user_image = UserImage.new(params[:user_image])
    unless user_image.nil?
      if @current_user.user_image.nil?
        if user_image.save
          @current_user.user_image = user_image
        end
      elsif @current_user.user_image.update_attributes(params[:user_image])
        flash.now[:notice] = '用户头像更新成功.'
      end
    end
    unless params[:imgpos].nil?
      pos = params[:imgpos].split('_')
      image = Magick::Image.read(Rails.public_path+@current_user.user_image.public_filename).first
      image.crop!(pos[0].to_i, pos[1].to_i, pos[2].to_i, pos[2].to_i)
      image.crop_resized!(60, 60,Magick::CenterGravity)
      image.write(Rails.public_path+@current_user.user_image.public_filename(:thumb))
      flash.now[:notice] = '用户头像更新成功.'
    end
    @user_info = UserInfo.find_or_create_by_user_id(@current_user.id)
    @user_image = UserImage.new
    render :action=>:edit_image
  end

  def setting
    @account = @current_user.account
    @user_info = UserInfo.find_or_create_by_user_id(@current_user.id)
    respond_to do |format|
      format.html {}
      format.js{render_to_facebox}
    end
  end

  def update_user_email
    @account = @current_user.account
    @account.email = params[:account][:email]
    if @account.save
      respond_to do |format|
        format.html do
          flash[:notice] = "E-mail修改成功！"
          redirect_to :action =>'setting'
          return
        end
        format.js do
          render :update do |page|
            page.replace_html 'current_email',"#{@account.email}"
          end
        end
      end
    else
      respond_to do |format|
        format.html do
          flash[:notice] = "E-mail修改失败！"
          redirect_to :action =>'setting'
          return
        end
        format.js{render :text => "alert('#{@account.errors.on :email}');"}
        #        format.js{render :text => "jQuery('#facebox .content').prepend('<div class=\"message\">sssssss</div>')"}
      end
    end
  end

  def update_user_password
    @account = @current_user.account
    if Account.authenticate(@account.login, params[:password])
      @account.password, @account.password_confirmation = params[:new_password], params[:new_password_confirmation]
      if @account.save
        respond_to do |format|
          format.html do
            flash[:notice] = "成功修改了密码！"
            redirect_to :action =>'setting'
          end
          format.js{render :text => "alert('密码修改成功！！');"}
        end
      end
    else
      respond_to do |format|
        format.html do
          flash[:notice] = "原始密码错误！"
          redirect_to :action =>'setting'
        end
        puts "="*30
        format.js do
          if params[:password].blank?
            render :text => "alert('请输入原始密码！');"
          else
            render :text => "alert('原始密码错误！');"
          end
        end
      end
    end
  end

  def my_courses
    @user = User.find(params[:id])
    @user_info = UserInfo.find_or_create_by_user_id(@user.id)
    @my_courses =  @user.my_courses
  end

  def favorates
    @user = User.find(params[:id])
    @user_info = UserInfo.find_or_create_by_user_id(@user.id)
    @my_courses =  @user.my_courses
  end

  private
  def find_current_user
    @current_user = current_user
  end
end
